<?php
namespace app\admin\controller;

use app\admin\model\Admin;
use app\common\controller\Backend;
use think\helper\Time;
use think\Session;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Ques extends Backend
{

    /**
     * QuesMerchant模型对象
     * @var \app\admin\model\QuesMerchant
     */
    protected $model = null;
    protected $sess = [];
    protected $whereType = [];

    public function _initialize()
    {
        parent::_initialize();
        $gtypeid =  $this->auth->gtypeid;
        $this -> assign('gtypeid', $gtypeid );
        $admin = Admin::get($this -> auth -> id);

        $controller = $this -> request -> controller();

        if($this -> auth -> id == 1823 && in_array($controller,['QuesOutside','Outside','QuesPermanent','Permanent','QuesMigrant','Migrant'])){
            $gtypeid = 1;
            $this -> assign('gtypeid', $gtypeid );
            $admin -> gtypeid = 1;
            $admin -> gid = 38;
        }

        $sess = [];
        //单位级登陆
        if($admin -> gtypeid == 1){
            $college_id = $admin -> gid;
            if($college_id){
                $sess['college_id'] = $college_id;
                //获取教职工id
                $teacherIds = (new \app\admin\model\Teacher())
                    -> alias('a')
                    -> join('department b','b.id=a.department_id and b.college_id='.$college_id)
                    -> column('a.id');

                //获取学生id
                $studentIds = (new \app\admin\model\Student())
                    -> alias('a')
                    -> where(['a.status' => 1])
                    -> join('classes b','a.classes_id=b.id and b.college_id='.$college_id)
                    -> column('a.id');
                $sess['teacherIds'] = $teacherIds;
                $sess['studentIds'] = $studentIds;

            }
        }elseif($admin -> gtypeid == 3){
            $classes_id = $admin -> gid;
            if($classes_id){
                $sess['classes_id'] = $classes_id;
                $studentIds = (new \app\admin\model\Student())
                    -> where(['classes_id'=>['=',$classes_id]])
                    -> where(['status' => 1])
                    -> column('id');
                $sess['studentIds'] = $studentIds;
            }
        }else{
//            $studentIds = (new \app\admin\model\Student())
//                -> where(['status' => 1])
//                -> column('id');
//            $sess['studentIds'] = $studentIds;
        }


        /**
         * 控制器规划：
         * 1非研究生-问卷上报：QuesStudent
         * 1非研究生-未完成统计：Student
         * 2研究生-问卷上报：QuesGraduate
         * 2研究生-未完成统计：Graduate
         * 1在编人员-问卷上报：QuesPermanent
         * 1在编人员-未完成统计：Permanent
         * 2编外人员-问卷上报：QuesOutside
         * 2编外人员-未完成统计：Outside
         * 3外来务工-问卷上报：QuesMigrant
         * 3外来务工-未完成统计：Migrant
         */


        if(in_array($controller,['QuesStudent','Student','QuesPermanent','Permanent']) && $admin -> gtypeid != 3){
            $this -> whereType['type']=1;
        }else if(in_array($controller,['QuesGraduate','Graduate','QuesOutside','Outside'])){
            $this -> whereType['type']=2;
        }else if(in_array($controller,['QuesMigrant','Migrant'])){
            $this -> whereType['type']=3;
        }


        $this -> assign('is_class',$admin -> gtypeid == 3 ? 1 : 0);

        $this -> sess = $sess;

    }


    public function report()
    {
        $gtypeid =  $this->auth->gtypeid;
        $college_id = $this->auth->gid;
        $type = $this->request->param("type");

        if($gtypeid !=1)
            $this->error('您没有权限上报');

        $model = new  \app\admin\model\CollegeReport;
        $today = Time::today();
        $row = $model -> where(['college_id'=>$college_id,'type'=>$type])->whereTime('report_time','between',$today)->find();
        if($row)
        {
            $row->is_report = 1;
            $row->report_time = time();
            $row->type = $type;
            $row->save();
            $this->success('上报成功');
        }
        $data = [
            'college_id' => $college_id,
            'is_report' => 1,
            'type' => $type,
            'report_time' => time()
        ];
        $res= $model->save($data);
        if($res)
            $this->success('上报成功');
        else
            $this->error('上报失败，请重新再试');
    }
}